<?php
namespace Model\Outlanderp4la;

use Hoa\Registry\Registry;
use Library\Db;
use Library\Log;
use Model\BaseModel;
use Requests;


/**
 * SkuModel
 * @author los_gsy
 */
class SkuModel extends BaseModel {
	/**
	 * 构造函数
	 */
	public function __construct() {
		parent::__construct();
	}


	/**
	 * test
	 */
	static public function test() {
		return __METHOD__;
	}


	/**
	 * 以sku的更新时间为起点，获取skusystem系统的sku列表
	 * @param string $skuUpdatedAt
	 * @param number $page
	 * @param number $pagesize
	 */
	static public function getSkuListFromSkusystemByUpdatedAt($skuUpdatedAt, $page = 1, $pagesize = 20) {
		//定义变量
		$conf = Registry::get('conf');
		$skusystem = $conf['outlanderp4la']['api']['skusystem'];

		//http接口参数
		$header = ['Accept' => 'application/json'];
		$option = ['auth' => [$skusystem['username'], $skusystem['passwd']]];
		$param = ['updatedAt' => $skuUpdatedAt, 'page' => $page, 'pagesize' => $pagesize];
		$url = $skusystem['url'] . '?' . http_build_query($param);
		Log::debug(__METHOD__ . ' url: ' . $url);

		//执行远程调用
		$resp = Requests::get($url, $header, $option);

		//如果返回http状态码不为200，则记录日志，并返回错误
		if ($resp->status_code != 200) {
			Log::critical(__METHOD__ . ' resp http status_code: ' . $resp->status_code);
			return false;
		}

		//返回正常，更新sku表
		$result = json_decode($resp->body, true);
		Log::debug(__METHOD__ . ' resp body: ' . var_export($result, true));

		//如果业务状态码不为200，则记录日志，并返回错误
		if ($result['status'] != 200) {
			Log::error(__METHOD__ . ' status : ' . $result['status']);
			Log::error(__METHOD__ . ' msg : ' . $result['msg']);
			Log::error(__METHOD__ . ' skuResult : ' . var_export($result['skuResult'], true));
			return false;
		}

		$ret = $result['skuResult']['list'];
		return $ret;
	}


	/**
	 * 通过code，查询sku
	 * @param unknown $code
	 */
	static public function getSkuByCode($code) {
		//定义变量
		$conf = Registry::get('conf');
		$dbConf = $conf['database']['outlanderp4la'];
		$db = new Db($dbConf);

		//查询sku
		$db->where('goods_sn', $code);
		$sku = $db->getOne('goods');

		//Success
		return $sku;
	}


	/**
	 * 新增sku
	 * @param array $sku
	 */
	static public function addSku($sku) {
		//定义变量
		$conf = Registry::get('conf');
		$dbConf = $conf['database']['outlanderp4la'];
		$db = new Db($dbConf);

		//新增sku
		$result = $db->insert('goods', $sku);

		//Success
		return $result;
	}


	/**
	 * 通过code，保存sku
	 * @param array $sku
	 * @param string $code
	 */
	static public function saveSkuByCode($sku, $code) {
		//定义变量
		$conf = Registry::get('conf');
		$dbConf = $conf['database']['outlanderp4la'];
		$db = new Db($dbConf);

		//保存sku
		$db->where('goods_sn', $code);
		$result = $db->update('goods', $sku);

		//Success
		return $result;
	}


	/**
	 * 新增或更新sku
	 * @param array $sku
	 * @param string $code
	 */
	static public function addOrSaveSkuByCode($sku, $code) {
		$existSku = self::getSkuByCode($code);
		if ($existSku) {
			$ret = self::saveSkuByCode($sku, $code);
			return $ret;
		}
		$ret = self::addSku($sku);
		return $ret;
	}



}